#Descriptive Statistics of Experiment 2
#Alan Yan
#June 1st

#clear environment
rm(list = ls())

#load libraries
library(pacman)
p_load(tidyverse,
       stargazer)

#load data
dt <- read.csv("02-Experiment-2/data/04-clean-data/clean_data.csv", stringsAsFactors = FALSE, header = TRUE)

#descriptive statistics of the sample
dt %>%
  mutate(
    Gender = 
      case_when(
        gender == "female" ~ "Woman",
        gender == "male" ~ "Man",
        is.na(gender) == TRUE ~ "Unknown"
      )
  ) -> dt

#right column Table S4
table(dt$Gender) %>%
  prop.table() %>%
  as.data.frame() %>%
  mutate(Proportion = paste0(round(Freq*100, 2), "%")) %>%
  select(1,3) %>%
  setNames(., c("Gender", "Proportion")) %>%
  stargazer(summary = FALSE,
            rownames = FALSE,
            title = "Descriptive Statistics, Study 2")

#descriptive statistics by experimental condition
dt <- dt %>%
  mutate(
    `Experimental Condition` =
      case_when(
        group.x == "female" ~ "Female-named",
        group.x == "male" ~ "Male-named",
        group.x == "gen.neutral" ~ "Ambiguously-named",
        group.x == "no.name" ~ "Unnamed"
      )
  )

dt %>%
  group_by(Gender) %>%
  filter(`Experimental Condition` == "Female-named") %>%
  summarise(n = n()) %>%
  mutate(freq = n / sum(n)*100) %>%
  mutate(Percent = paste0(round(freq, 2), "%")) %>%
  select(4) %>%
  t() %>%
  as.data.frame() %>%
  cbind("Female-named", .) %>%
  setNames(., c("Experimental Condition", "Men", "Unknown Gender", "Women")) -> female.exp.cond

dt %>%
  group_by(Gender) %>%
  filter(`Experimental Condition` == "Male-named") %>%
  summarise(n = n()) %>%
  mutate(freq = n / sum(n)*100) %>%
  mutate(Percent = paste0(round(freq, 2), "%")) %>%
  select(4) %>%
  t() %>%
  as.data.frame() %>%
  cbind("Male-named", .) %>% 
  setNames(., c("Experimental Condition", "Men", "Unknown Gender", "Women")) -> male.exp.cond

dt %>%
  group_by(Gender) %>%
  filter(`Experimental Condition` == "Ambiguously-named") %>%
  summarise(n = n()) %>%
  mutate(freq = n / sum(n)*100) %>%
  mutate(Percent = paste0(round(freq, 2), "%")) %>%
  select(4) %>%
  t() %>%
  as.data.frame() %>%
  cbind("Ambiguously-named", .) %>% 
  setNames(., c("Experimental Condition", "Men", "Unknown Gender", "Women")) -> ambig.exp.cond

dt %>%
  group_by(Gender) %>%
  filter(`Experimental Condition` == "Unnamed") %>%
  summarise(n = n()) %>%
  mutate(freq = n / sum(n)*100) %>%
  mutate(Percent = paste0(round(freq, 2), "%")) %>%
  select(4) %>%
  t() %>%
  as.data.frame() %>%
  cbind("Unnamed", .) %>% 
  setNames(., c("Experimental Condition", "Men", "Unknown Gender", "Women")) -> unnamed.exp.cond

#Table S6
rbind(female.exp.cond,
      male.exp.cond,
      ambig.exp.cond,
      unnamed.exp.cond) %>%
  stargazer(summary = FALSE,
            rownames = FALSE,
            title = "Descriptive Statistics by Experimental Condition, Study 2")


